package org.nobody.mallb2c.mallproduct.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.nobody.mallb2c.mallproduct.entity.AttrEntity;
import org.nobody.mallb2c.mallproduct.service.AttrAttrgroupRelationService;
import org.nobody.mallb2c.mallproduct.service.AttrService;
import org.nobody.mallb2c.mallproduct.service.CategoryService;
import org.nobody.mallb2c.mallproduct.vo.AttrGroupRelationVo;
import org.nobody.mallb2c.mallproduct.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.nobody.mallb2c.mallproduct.entity.AttrGroupEntity;
import org.nobody.mallb2c.mallproduct.service.AttrGroupService;
import org.nobody.common.utils.PageUtils;
import org.nobody.common.utils.R;


/**
 * 属性分组
 *
 * @author fancier
 * @email fancier2093@gmail.com
 * @date 2020-07-30 16:25:27
 */
@RestController
@RequestMapping("mallproduct/attrgroup")
public class AttrGroupController {
  @Autowired
  private AttrGroupService attrGroupService;

  @Autowired
  private CategoryService categoryService;

  @Autowired
  AttrService attrService;

  @Autowired
  AttrAttrgroupRelationService attrAttrgroupRelationService;

  @PostMapping("/attr/relation")
  public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){
    attrAttrgroupRelationService.saveBatch(vos);

    return R.ok();
  }

  @GetMapping("/{catelogId}/withattr")
  public R getAttrGroupWithAttrs(@PathVariable("catelogId")Long catelogId){
    //1、 查出当前分类下的所有属性分组
    //2、 查出每个属性分组的所有属性
    List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);

    return R.ok().put("data", vos);
  }

  /**
   * 查询分组关联的所有属性
   * @param attrgroupId
   * @return
   */
  @GetMapping("/{attrgroupId}/attr/relation")
  public R attrRelation(
    @PathVariable("attrgroupId")
      Long attrgroupId
  ) {
    List<AttrEntity> entities = attrService.getRelationAttr(attrgroupId);
    return R.ok().put("data", entities);
  }

  /**
   * 获取当前分组没有关联的所有属性
   * @param attrgroupId
   * @return
   */
  @GetMapping("/{attrgroupId}/noattr/relation")
  public R attrNoRelation(
    @RequestParam Map<String, Object> params,
    @PathVariable("attrgroupId")
    Long attrgroupId
  ) {
    PageUtils page = attrService.getNoRelationAttr(params,attrgroupId);
    return R.ok().put("page", page);
  }

  /**
   * 列表
   */
  @RequestMapping("/list/{catelogId}")
  //@RequiresPermissions("mallproduct:attrgroup:list")
  public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId) {
    //PageUtils page = attrGroupService.queryPage(params);

    PageUtils page = attrGroupService.queryPage(params, catelogId);

    return R.ok().put("page", page);
  }


  /**
   * 信息
   */
  @RequestMapping("/info/{attrGroupId}")
  //@RequiresPermissions("mallproduct:attrgroup:info")
  public R info(@PathVariable("attrGroupId") Long attrGroupId) {
    AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
    Long catelogId = attrGroup.getCatelogId();
    Long[] path = categoryService.findCatelogPath(catelogId);

    attrGroup.setCatelogPath(path);

    return R.ok().put("attrGroup", attrGroup);
  }

  /**
   * 保存
   */
  @RequestMapping("/save")
  //@RequiresPermissions("mallproduct:attrgroup:save")
  public R save(@RequestBody AttrGroupEntity attrGroup) {
    attrGroupService.save(attrGroup);

    return R.ok();
  }

  @PostMapping("/attr/relation/delete")
  public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos){
    attrService.deleteRelation(vos);
    return R.ok();
  }

  /**
   * 修改
   */
  @RequestMapping("/update")
  //@RequiresPermissions("mallproduct:attrgroup:update")
  public R update(@RequestBody AttrGroupEntity attrGroup) {
    attrGroupService.updateById(attrGroup);

    return R.ok();
  }

  /**
   * 删除
   */
  @RequestMapping("/delete")
  //@RequiresPermissions("mallproduct:attrgroup:delete")
  public R delete(@RequestBody Long[] attrGroupIds) {
    attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

    return R.ok();
  }

}
